package com.huayue.controller;

import static com.huayue.util.PhoneHelper.map;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.Customer;
import com.huayue.service.CustomerService;
import com.huayue.util.PageHelper;

/**
 * 顾客信息
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService svc;

    BaseController bc = new BaseController();

    /**
     * 测试用顾客验证码
     * 
     * @param Customer
     * @return
     */
    @RequestMapping("/yzm")
    @ResponseBody
    public String yzm(Customer Customer) {
        DefaultProfile profile =
            DefaultProfile.getProfile("cn-hangzhou", "LTAI4FdDACLC6BKU8qV35qeU", "kT5yJZvbOHpxwb5y1vhM7GZV5LmMrN");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", Customer.getUphone());
        request.putQueryParameter("SignName", "华悦");
        request.putQueryParameter("TemplateCode", "SMS_180049815");
        long code = Math.round((Math.random() + 1) * 1000);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            map.put(Customer.getUphone(), String.valueOf(code));
            String str = response.getData().substring(12, 14);
            System.out.println(str);
            if ("OK".equals(str)) {
                return "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

    /**
     * 测试用顾客注册
     * 
     * @param Customer
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public String register(Customer Customer) {
        map.put("123", "123");
        try {
            if (Customer.getYzm().equals(map.get(Customer.getUphone()).toString())) {
                svc.insert(Customer);
                return "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

    /**
     * 测试用顾客登录
     * 
     * @param Customer
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(Customer Customer) {
        int count = svc.finduser(Customer);
        if (count == 0) {
            return "false";
        } else {
            return "true";
        }
    }

    @LogAnno(operateType = "添加-顾客信息")
    @RequestMapping("/add")
    public ModelAndView add(Customer customer) {
        return bc.add(svc, customer);
    }

    @LogAnno(operateType = "修改-顾客信息")
    @RequestMapping("/update")
    public ModelAndView update(Customer customer) {
        return bc.update(svc, customer);
    }

    @LogAnno(operateType = "删除-房间类型")
    @RequestMapping("/delete")
    public ModelAndView delete(Customer customer) {
        return bc.delete(svc, customer);
    }

    @RequestMapping(value = "/findOne", produces = {"text/javascript;charset=UTF-8"})
    @ResponseBody
    public ModelAndView findOne(Customer customer) {
        return bc.findOne(svc, customer);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, Customer customer) {
        return bc.findPage(svc, page, customer, customer);
    }

    /**
     * 根据手机号查找用户信息 避免员工手机号重复
     *
     * @param customer
     * @return
     */
    @RequestMapping("/findOnePhone")
    public ModelAndView findEmpByPhone(Customer customer) {
        FastJsonJsonView view = new FastJsonJsonView();
        Map<String, Object> map = svc.findOnePhone(customer);
        if (map != null) {
            view.addStaticAttribute("status", 300);
            view.addStaticAttribute("msg", "已存在该账号");
            view.addStaticAttribute("data", map);
        } else {
            view.addStaticAttribute("status", 200);
            view.addStaticAttribute("msg", "可用");
        }
        return new ModelAndView(view);
    }

}
